//
//  ViewController.swift
//  PhotoSet
//
//  Created by 金玉龙 on 15/12/17.
//  Copyright © 2015年 jinyulong. All rights reserved.
//

import UIKit

var baseUrl = "http://c.3g.163.com/photo/api/related/0096/84688.json"
var baseUrl2 = "http://c.3g.163.com/photo/api/related/0096/84445.json"

class PSMainViewController: UIViewController,UITableViewDelegate ,UITableViewDataSource{
    var listTableView : UITableView!
    var dataArray : NSMutableArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self .initProperties()
        self .initCompnent()
        self .loadData()
        
    }
    
    private func initProperties(){
        self.title = "图集Swift"
        self.navigationController?.navigationBar.translucent = false
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "刷新", style: UIBarButtonItemStyle.Done, target: self, action: "refresh")
        self.view.backgroundColor = UIColor.whiteColor()
    }
    
    private func initCompnent(){
        
        self.listTableView = UITableView.init(frame: UIScreen.mainScreen().bounds)
        self.listTableView.backgroundColor = UIColor.whiteColor()
        self.listTableView.delegate = self
        self.listTableView.dataSource = self
        self.view .addSubview(self.listTableView)
    }
//mark: TableViewDelegate
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier = "photoCell"
        var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? PSCell
        if cell == nil{
            cell = PSCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: identifier)
        }
        return cell!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let showCtrl = PSShowViewController()
        self.navigationController!.pushViewController(showCtrl, animated: true)
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 149
    }
    
    
    private func loadData(){
        let session = NSURLSession.sharedSession()
        let dataTask = session .dataTaskWithURL(NSURL.init(string: baseUrl)!) { (data , response , error) -> Void in
            if data != nil{
                print(data)
                
            }
        }
        dataTask.resume()
    }
    
    func refresh(){
        self.listTableView .reloadData()
    }

    
    override func shouldAutorotate() -> Bool {
        return false
    }
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

